package com.beeasy.hzbpm;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.beeasy.hzbpm.bpm.BpmMQ;
import com.github.llyb120.nami.core.Async;
import com.github.llyb120.nami.core.Config;
import com.github.llyb120.nami.core.Nami;

import java.util.concurrent.CountDownLatch;

import static com.beeasy.hzbpm.bean.MongoService.db;

public class App {

    public static void main(String[] args) throws InterruptedException {
        String config = null;
        String importUserFile = "";
        if(args.length > 0){
            for (int i = 0; i < args.length; i++) {
                if(args[i].equals("-c")){
                    config = args[++i];
                }
                if(args[i].equals("-iu")){
                    importUserFile = args[++i];
                }
            }
        }
        if (config == null) {
            config = "./nami.conf";
        }
        Nami.start(config, true);

        //开启mq服务
        BpmMQ.start();

        db.getName();
        if(!importUserFile.isEmpty()){
            String finalImportUserFile = importUserFile;
            Async.execute(() -> {
                Thread.sleep(3000);
                HttpUtil.createPost("http://127.0.0.1:" + Config.config.port + "/bpm/sync/sync")
                    .header("Content-Type","application/json")
                    .header("bpm-token", Config.config.var.ss("sync-token"))
                    .body(FileUtil.readUtf8String(finalImportUserFile))
                    .executeAsync();
            });
        }
//        Document item = db.getCollection("user")
//            .aggregate(a(o("id", "1")))
//            .first();
//        System.out.println(Json.stringify(item));

        CountDownLatch cd = new CountDownLatch(1);
        cd.await();
    }
}

